﻿using OpenCvSharp;
using System.Drawing;
using System.Runtime.InteropServices;
using VIA.ImageProcessing;
using VIA.Integration.Domain;
using VT.Framework.Scene2D.Structs;

namespace VIA.SiMoJi.Domain.Vision
{
    public class DrawablePoint2 : DrawablePoint
    {
        public DrawablePoint2() { }
        public DrawablePoint2(VIAPoint point) : base(point)
        {

        }
        public DrawablePoint2(double x, double y) : this(new VIAPoint(x, y))
        {
        }

        public int Size { get; set; } = 10;
        public override void Draw(Graphics g)
        {
            System.Drawing.Pen pen = new System.Drawing.Pen(base.Foreground, base.StrokeThickness);
            g.DrawLine(pen, new System.Drawing.Point((int)Point.X - Size, (int)Point.Y), new System.Drawing.Point((int)Point.X + Size, (int)Point.Y));
            g.DrawLine(pen, new System.Drawing.Point((int)Point.X, (int)Point.Y - Size), new System.Drawing.Point((int)Point.X, (int)Point.Y + Size));
            if (!base.Text.IsNullOrEmpty())
            {
                g.DrawString(base.Text, new Font("微软雅黑", base.FontSize), pen.Brush, new PointF((float)Point.X, (float)(Point.Y - (double)base.FontSize - 1.0)));
            }
        }
    }

    public static class VTExtensions
    {
        public static VIAPoint ToVIAPoint(this VPoint pt)
        {
            if (pt == null)
            {
                return null;
            }
            return new VIAPoint(pt.X, pt.Y);
        }

        public static VIALine ToVIALine(this VLine line)
        {
            if (line == null)
            {
                return null;
            }
            return new VIALine(line.P1.ToVIAPoint(), line.P2.ToVIAPoint());
        }

        public static VIACircle ToVIACircle(this VCircle circle)
        {
            if (circle == null) return null;
            return new VIACircle(new VIAPoint(circle.CenterX, circle.CenterY), circle.Radius);
        }

        public static DrawablePoint ToDrawablePoint(this VPoint point)
        {
            if (point == null) return null;
            return new DrawablePoint(point.ToVIAPoint());
        }

        public static DrawablePoint2 ToDrawablePoint2(this VPoint point)
        {
            if (point == null) return null;
            return new DrawablePoint2(point.ToVIAPoint());
        }

        public static DrawableLine ToDrawableLine(this VLine line)
        {
            if (line == null)
            {
                return null;
            }
            return new DrawableLine(line.ToVIALine());
        }

        //public static ToVIARect(this VRectangle rect)
        //{
        //    if (rect == null)
        //    {
        //        return null;
        //    }
        //    return new VIARect(rect.CenterX, rect.CenterY, rect.Width, rect.Height, rect.Angle);
        //}

        //public static VIAPolygon ToVIAPolygon(this VDefectResult defectResult)
        //{
        //    if (defectResult.Contour == null || defectResult.Contour.Count == 0) return null;
        //    List<VIAPoint> listPts = defectResult.Contour.Select(x => x.ToVIAPoint()).ToList();
        //    VIAPolygon vIAPolygon = new VIAPolygon(listPts);
        //    return vIAPolygon;
        //}

        //public static VIAPolygon ToVIAPolygon(this VPath vPath)
        //{
        //    List<VIAPoint> listPts = vPath.Data.Select(x => x.ToVIAPoint()).ToList();
        //    VIAPolygon vIAPolygon = new VIAPolygon(listPts);
        //    return vIAPolygon;
        //}


        public static int[] CalcHist(ImageData3DU image)
        {
            int count = 65535;
            ushort max_ = image.Data.Max();
            //float bandWidth = max_ * 1.0f / count;
            using Mat imageMat = image.ToMat();
            using Mat hist = new();
            Mat[] mats0 = new Mat[] { imageMat };
            int[] channels = new int[] { 0 };
            int[] histsize = new int[] { count };
            Rangef[] range = new Rangef[1];//一个通道，范围
            range[0] = new Rangef(0, ushort.MaxValue);//从0开始（含）,到256结束（不含）
            //Mat mask = new Mat();//不做掩码

            Cv2.CalcHist(mats0, channels, null, hist, 1, histsize, range);
            float[] histData = new float[hist.Height];
            Marshal.Copy(hist.Data, histData, 0, hist.Height);
            histData[0] = 0;
            return histData.Select(x => Convert.ToInt32(x)).ToArray();
        }
    }
}
